package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Article;
import com.example.domain.entity.Type;
import com.example.exception.FloraException;
import com.example.mapper.TypeMapper;
import com.example.service.ArticleService;
import com.example.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Type)表服务实现类
 *
 * @author makejava
 * @since 2022-12-03 14:47:23
 */
@Service("typeService")
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {
    @Autowired
    private ArticleService articleService;

    /** 后台管理员查看所有类型（禁用的+未禁用的） */
    @Override
    public IPage<Type> getTypePage(String name, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Type> qw = new LambdaQueryWrapper<>();
        Page<Type> page = new Page<>(pageNum,pageSize);
        if(name!=null){
            qw.like(Type::getName,name);
        }

        return this.baseMapper.selectPage(page,qw);
    }

    /** 前台查看 未被禁用 的文章类型 */
    @Override
    public List<Type> getTypeList() {
        LambdaQueryWrapper<Type> qw = new LambdaQueryWrapper<>();
        qw.eq(Type::getStatus,0);
        return this.baseMapper.selectList(qw);
    }

    /** 添加文章类型 */
    @Override
    public Type addType(Type type) throws FloraException {
//        if(Type.getId()<0){
//            throw new FloraException("文章分类id错误");
//        }
        try{
            this.baseMapper.insert(type);
            return type;
        } catch (Exception e) {
            throw new FloraException("添加文章类型失败");
        }
    }

    @Override
    public void updateTypeCommon(Type type) throws FloraException {
//        if(type.getId()<0){
//            throw new FloraException("分类id错误");
//        }
        try{
            this.baseMapper.updateById(type);
        } catch (Exception e) {
            throw new FloraException("修改文章类型失败");
        }
    }

    /** 要先将有关的文章typeId修改 */
    @Override
    public void updateTypeStatus(Type type) throws FloraException {
        Long typeId = type.getId();
        LambdaQueryWrapper<Article> qw = new LambdaQueryWrapper<>();

        //恢复使用
        if(type.getStatus()==0){
            qw.eq(Article::getTypeId,typeId*(-1));
        }
        //status==1,禁用
        else{
            qw.eq(Article::getTypeId,typeId);
        }

        List<Article> articleList = articleService.list(qw);

        /** 将所有该分类的文章 分类id 设置为原来的相反数
         *  文章的TypeId字段：  正数：正常使用的  负数：禁用的  0：已删除的 */
        for(Article article:articleList){
            Long cid=article.getTypeId();
            article.setTypeId(cid*(-1));
            articleService.updateById(article);
        }

        try{
            this.baseMapper.updateById(type);
        } catch (Exception e) {
            throw new FloraException("修改文章分类失败");
        }
    }

    /** 删除分类时，将所有与之相关的文章分类id设为0 */
    @Override
    public void deleteType(Long id) throws FloraException {
        LambdaQueryWrapper<Article> qw = new LambdaQueryWrapper<>();
        qw.eq(Article::getTypeId,id);
        List<Article> articleList = articleService.list(qw);

        for(Article article:articleList){
            article.setTypeId((long) 0);
            articleService.updateById(article);
        }

        try{
            this.baseMapper.deleteById(id);
        } catch (Exception e) {
            throw new FloraException("删除文章分类失败"+e.getMessage());
        }
    }
}

